package org.netEase.controller;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.netEase.dao.adverDao;
import org.netEase.dao.impl.adverDaoImple;
import org.netEase.entity.adver;

import com.jspsmart.upload.File;
import com.jspsmart.upload.Files;
import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

@WebServlet(urlPatterns = { "/backstageManager/uadatetupian.shtml" })
public class upTuPian extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String imid=request.getParameter("Id");
		
		adverDao adDao=new adverDaoImple();
		adver list=adDao.getUp(Integer.parseInt(imid));
		request.setAttribute("list", list);
		request.getRequestDispatcher("/backstageManager/picture-extil.jsp").forward(request, response);
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		SmartUpload smartUpload = new SmartUpload();
		smartUpload.initialize(this,request,response);
		try {
			smartUpload.upload();
		} catch (SmartUploadException e) {
			e.printStackTrace();
		}

		request.setCharacterEncoding("UTF-8");
		Request req = smartUpload.getRequest();
		String img = req.getParameter("img");
		String state=req.getParameter("state");
		String imid = req.getParameter("ids");
		Files files = smartUpload.getFiles();
		File file = files.getFile(0);
		adver ad=new adver();
		ad.setImg(img);
		ad.setState(state);
		ad.setImid(Integer.parseInt(imid));
		if(file.isMissing() == false){
			try {
				file.saveAs("./images/index/"+file.getFileName());
			} catch (SmartUploadException e) {
				e.printStackTrace();
			}
		
			ad.setImg(file.getFileName());
		}
		adverDao adDao=new adverDaoImple();
		int result=adDao.update(ad);
		
		if(result>0){
			response.sendRedirect(request.getContextPath() + "/backstageManager/adver.shtml");
		}
		
		
	}

}
